const cart = require('../model/cartModel.js');

async function add(ctx){
    let data = {
        gid: ctx.request.body.id,
        uid: ctx.user.id,
        amount: ctx.request.body.amount
      }
      let result = await cart.add(data)
      ctx.body = result
}

async function list(ctx) {
    let uid = ctx.user.id;
    let result = await cart.list(uid)
    if (result.total) {
      ctx.body = { code: 1, msg: '', data: result }
    } else {
      ctx.body = { code: 0, msg: '暂无数据', data: {lists:[]}}
    }
  }
  
  async function mod(ctx) {
    let data = {
      id: ctx.params.id,
      amount: ctx.request.body.amount
    }
    let res = await cart.mod(data)
    if (res.code) {
      ctx.body = res;
    } else {
      ctx.body = res
    }
  }
  
  async function del(ctx) {
    
      console.log(ctx.request.query.id);
      let ids=ctx.request.query.id
      let res= await cart.del(ids)
    if (res.code) {
      ctx.body = res;
    } else {
      ctx.body = res;
    }
  }

module.exports = { add ,list,mod,del}